package com.huage.courseplatform.framework.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huage.courseplatform.framework.config.RestTemplateConfig;
import com.huage.courseplatform.framework.model.entity.WxMsgTemplateData;
import com.huage.courseplatform.framework.utils.http.HttpUtils;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信请求工具类
 */
public class WechatUtil {

    //小程序appId
    private static final String APP_ID ="wx16f324818d11dd7d";
    //小程序密钥
    private static final String SECRET ="f8e7dd58fa5518db372e3e30bbf83d32";

    //公众号appId
    private static final String MP_APP_ID ="wxc90b5084ba49a353";



    /**
     * 获取openId
     * @param code
     * @return
     */
    public static JSONObject getSessionKeyOrOpenId(String code) {
        String requestUrl ="http://api.weixin.qq.com/sns/jscode2session";
        String url = "https://api.weixin.qq.com/sns/jscode2session" + "?appid={appId}&secret={secret}&js_code={code}&grant_type={authorizationCode}";
        String authorizationCode = "authorization_code";
        Map<String, String> requestUrlParam = new HashMap<>();
        requestUrlParam.put("appId", APP_ID);
        requestUrlParam.put("secret", SECRET);
        requestUrlParam.put("code",code);
        requestUrlParam.put("authorizationCode",authorizationCode);
        RestTemplate restTemplate = null;
        try {
            restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(restTemplate.getForObject(url, String.class, requestUrlParam));
        return jsonObject;
    }

    /**
     * 获取token
     * @return
     */
    public static JSONObject getAccessToken(){
        String requestUrl ="https://api.weixin.qq.com/cgi-bin/token";
        String clientCredential = "client_credential";
        String param = "grant_type="+clientCredential+"&appid="+APP_ID+"&secret="+SECRET;

        String result =  HttpUtils.sendGet(requestUrl,param);
        JSONObject jsonObject = JSONObject.parseObject(result);

        return jsonObject;
    }


    /**
     * 该接口用于下发小程序和公众号统一的服务消息
     * @param token token
     * @param openId 发送对象openId
     * @param templateId 模板id
     * @param data 模板数据
     * @param appPath 跳转小程序路径
     * @return
     */
    public static JSONObject sendUniformMessage(String token,String openId,String templateId,JSONObject data,String appPath){
        String requestUrl ="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send"+"?access_token="+token;

        JSONObject  miniprogram = new JSONObject();
        miniprogram.put("appid",APP_ID);
        miniprogram.put("pagepath",appPath);

        JSONObject  mpTemplateMsg = new JSONObject();
        mpTemplateMsg.put("data",data);
        mpTemplateMsg.put("miniprogram",miniprogram);
        mpTemplateMsg.put("template_id",templateId);
        mpTemplateMsg.put("appid", MP_APP_ID);

        JSONObject paramObject = new JSONObject();
        paramObject.put("touser",openId);
        paramObject.put("mp_template_msg",mpTemplateMsg);

        String result =  HttpUtils.sendPost(requestUrl,paramObject.toJSONString());
        JSONObject jsonObject = JSONObject.parseObject(result);

        return jsonObject;
    }

}

